package com.lancoo.answer.ui.ques.quesBody;

import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.lancoo.answer.R;
import com.lancoo.answer.model.AnswerConstant;
import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.bean.ExamViewBean;
import com.lancoo.answer.model.bean.Ques;
import com.lancoo.answer.util.ExamFontSizeAdjustUtils;
import com.lancoo.answer.util.RichTextUtils;
import com.lancoo.answer.widget.combinationView.ListenArticleView;

import java.util.List;

/**
 * Created by pengmi on 2021/12/16 9:30
 * Description:听力原文题干内容fragment
 */
public class ListenArticleQuseBodyFragment extends Fragment {

    private static final String TAG = "ListenArticleQuseBodyFr";
    private View convertView;

    public static Fragment loadQueseFragment(int kindIndex, int queseIndex){
        ListenArticleQuseBodyFragment queseFragment=new ListenArticleQuseBodyFragment();
        Bundle mBundle=new Bundle();
        mBundle.putInt(AnswerConstant.KEY_TYPEINDEX,kindIndex);
        mBundle.putInt(AnswerConstant.KEY_QUESINDEX,queseIndex);
        queseFragment.setArguments(mBundle);
        return queseFragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        convertView= inflater.inflate(R.layout.ev_fragment_listen_article_ques_body,container,false);
        return convertView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if(getArguments()==null){
            return;
        }
        Log.e(TAG, "onViewCreated: " );
        int typeIndex = getArguments().getInt(AnswerConstant.KEY_TYPEINDEX);
        int quesIndex = getArguments().getInt(AnswerConstant.KEY_QUESINDEX);
        ExamViewBean constantExamBean = ConstantBean.Companion.getConstantExamBean();
        if(constantExamBean==null||
            constantExamBean.getTypeList()==null){
            return;
        }
        List<Ques> quesList = constantExamBean.getTypeList().get(typeIndex).getQuesList();
        if(quesList==null){
            return;
        }

        Ques ques = quesList.get(quesIndex);
        String article = "";
        if(!TextUtils.isEmpty(ques.getAudioArticle())){
            article=ques.getAudioArticle();
        }
        ListenArticleView listenArticleView=view.findViewById(R.id.listen_article_view);
        listenArticleView.setArticle(article,ques.getIsIndentArticle());
        TextView tv_subLeader=view.findViewById(R.id.tv_audio_sub_leader);
        if(!TextUtils.isEmpty(ques.getAudioSubLeader())){
            tv_subLeader.setVisibility(View.VISIBLE);
            tv_subLeader.setText(RichTextUtils.getQueseAnalysisCharSequence(Html.fromHtml(ques.getAudioSubLeader())));
        }
        adjustFontSize();

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    public void adjustFontSize(){
        if(convertView==null){
            return;
        }
        TextView textView = convertView.findViewById(R.id.tv_audio_sub_leader);
        ExamFontSizeAdjustUtils.adjustTextViewFontSize(textView,14);
        ListenArticleView listenArticleView=convertView.findViewById(R.id.listen_article_view);
        listenArticleView.adjustFontSize();

    }
}
